package cn.iocoder.yudao.module.crossborder.platform.controller.admin.amazon;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.crossborder.platform.service.provider.amazon.AmazonBusinessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - Amazon平台管理")
@RestController
@RequestMapping("/crossborder/amazon")
@Validated
public class AmazonController {

    @Resource
    private AmazonBusinessService amazonBusinessService;

    @PostMapping("/store/setup")
    @Operation(summary = "设置Amazon店铺")
    @PreAuthorize("@ss.hasPermission('crossborder:amazon:store:setup')")
    public CommonResult<Map<String, Object>> setupStore(@RequestParam("storeId") Long storeId,
                                                        @RequestBody Map<String, Object> config) {
        Map<String, Object> result = amazonBusinessService.setupAmazonStore(storeId, config);
        return success(result);
    }

    @PostMapping("/sync/execute")
    @Operation(summary = "执行Amazon数据同步")
    @PreAuthorize("@ss.hasPermission('crossborder:amazon:sync:execute')")
    public CommonResult<Map<String, Object>> executeSync(@RequestParam("storeId") Long storeId,
                                                         @RequestParam("syncType") String syncType,
                                                         @RequestParam(value = "startTime", required = false) LocalDateTime startTime,
                                                         @RequestParam(value = "endTime", required = false) LocalDateTime endTime) {
        Map<String, Object> result = amazonBusinessService.executeSync(storeId, syncType, startTime, endTime);
        return success(result);
    }

    @GetMapping("/store/overview")
    @Operation(summary = "获取Amazon店铺概览")
    @Parameter(name = "storeId", description = "店铺ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:amazon:store:query')")
    public CommonResult<Map<String, Object>> getStoreOverview(@RequestParam("storeId") Long storeId) {
        Map<String, Object> overview = amazonBusinessService.getStoreOverview(storeId);
        return success(overview);
    }

}
